package org.dbstar.scheduler.control.db.dependency;

import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONSerializer;

import org.apache.commons.lang.StringUtils;
import org.dbstar.scheduler.beans.JobDependency;
import org.dbstar.scheduler.beans.JobDependencyItem;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;


/**
 * JobDependency的Update对象
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-6-14
 */
public class JobDependencyUpdate extends SqlUpdate {
	public JobDependencyUpdate(JdbcTemplate jdbcTemplate) {
		super();
		setJdbcTemplate(jdbcTemplate);
		setSql("UPDATE SCHD_JOB_DEPENDENCY SET NAME=?, DESCRIPTION=?, KEY_SIZE=?, KEY_DATA=?, PARAMETER_SIZE=?, PARAMETER_DATA=?, DEPENDENCY_SIZE=?, DEPENDENCY_DATA=?, MODIFY_TIME=?, OWNER=?, MODIFIER=? WHERE SYSTEM_IDENTIFIER=? AND SEQUENCE=?");
		declareParameter(new SqlParameter("NAME", Types.VARCHAR));
		declareParameter(new SqlParameter("DESCRIPTION", Types.VARCHAR));
		declareParameter(new SqlParameter("KEY_SIZE", Types.INTEGER));
		declareParameter(new SqlParameter("KEY_DATA", Types.VARCHAR));
		declareParameter(new SqlParameter("PARAMETER_SIZE", Types.INTEGER));
		declareParameter(new SqlParameter("PARAMETER_DATA", Types.VARCHAR));
		declareParameter(new SqlParameter("DEPENDENCY_SIZE", Types.INTEGER));
		declareParameter(new SqlParameter("DEPENDENCY_DATA", Types.VARCHAR));
		declareParameter(new SqlParameter("MODIFY_TIME", Types.BIGINT));
		declareParameter(new SqlParameter("OWNER", Types.VARCHAR));
		declareParameter(new SqlParameter("MODIFIER", Types.VARCHAR));
		declareParameter(new SqlParameter("SYSTEM_IDENTIFIER", Types.VARCHAR));
		declareParameter(new SqlParameter("SEQUENCE", Types.INTEGER));
	}

	public int updateJobDependency(JobDependency dep) {
		long modifyTime = System.currentTimeMillis();
		int result = update(dep.getName(), dep.getDescription(), dep.getParameterSize(), parseKeysJson(dep.getKeys()),
				parseParametersJson(dep.getParameters()), parseDependenciesJson(dep.getDependencies()), modifyTime,
				dep.getOwner(), dep.getModifier(), dep.getIdentifier().getSystemIdentifier(), dep.getIdentifier()
						.getSequence());
		dep.setModifyTime(modifyTime);
		return result;
	}

	private String parseKeysJson(List<String> keys) {
		if (keys == null || keys.isEmpty()) {
			return null;
		} else {
			return StringUtils.join(keys, ',');
		}
	}

	private String parseParametersJson(Map<String, String> parameters) {
		if (parameters == null || parameters.isEmpty()) {
			return null;
		} else {
			return JSONSerializer.toJSON(parameters).toString();
		}
	}

	private String parseDependenciesJson(Set<JobDependencyItem> dependencies) {
		if (dependencies == null || dependencies.isEmpty()) {
			return null;
		} else {
			return JSONSerializer.toJSON(dependencies, new JobDependencyItemJsonConfig()).toString();
		}
	}
}
